<?php

class BasePlugin extends Yaf_Plugin_Abstract {
    public function routerStartup(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response) {
        $no_mobile = $request->getCookie ( "nomobile" );
        if(empty($no_mobile)){
            $detect = new third_Mobile_Detect();
            define('ISMOBILE', $detect->isMobile());
        }else{
            define('ISMOBILE', false);
        }
	}

	public function routerShutdown(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response) {
        //验证用户登录情况
        $params = $request->getParams();
        $cName = $request->getControllerName();
        $aName = $request->getActionName();
        $config = Yaf_Registry::get ( "config" )->toArray();
        $smatyConfig = $config ['smarty'];
        if(!empty($smatyConfig['template_dir'])){
            $template_path = $smatyConfig['template_dir'];
        }else{
            $template_path = $config['application']['directory'] . '/views/';
        }
        if(ISMOBILE){
            if(!empty($smatyConfig['mobile_template_dir'])){
                $template_path = $smatyConfig['mobile_template_dir'];

            }else{
                $template_path = $config['application']['directory'] . '/mviews/';
            }
        }
        if (!empty($smatyConfig['mobile_template_dir'])) unset($smatyConfig['mobile_template_dir']);
        $smatyConfig['template_dir'] = $template_path;
        SmartyAdapter::setConfig ( $smatyConfig );

        SmartyAdapter::assign('controllerName', strtolower($cName));
        SmartyAdapter::assign('actionName', strtolower($aName));
        //设置模板名
        $defaultTtemplateName = strtolower($cName.'/'.$aName);
        SmartyAdapter::$templateName = $defaultTtemplateName;

        //查询接口必须是ajax访问，且有吞吐量和ip访问限制
        if($cName == 'Searchcases' && $aName == 'search'){
            if(!$request->isXmlHttpRequest()){
                AjaxTool::returnJsonData(400);
            }
            //校验ip
            $spam = Lib_Tool::check_ip('web', 'serchCase');
            if ($spam !== true) {
                AjaxTool::returnJsonData(400,'当前IP访问频率过大，拒绝访问');
            }


        }
        $arrCacheConfig = TemplateModel::$cache;
        //判断是否要缓存

        if (isset($arrCacheConfig[$defaultTtemplateName]) && $tplCache = $arrCacheConfig[$defaultTtemplateName]) {
            //需要缓存
            if ($tplCache['cache']) {
                //配置缓存
                $cacheConfig = array(
                    'cache' => true,
                    'lifeTime' => $tplCache['lifeTime'],
                );
                //assign key
                if (isset($tplCache['key'])) {
                    $cacheConfig['key'] = array();
                    foreach ($tplCache['key'] as $key) {
                        if ($params[$key]) {
                            $cacheConfig['key'][] = $params[$key];
                        }
                    }
                }

                //设置缓存
                SmartyAdapter::setCacheConfig($cacheConfig);
                $isCached = SmartyAdapter::isCached();

                if ($isCached) {
                    //显示缓存的模板
                    SmartyAdapter::display();
                    //直接退出，不需要请求数据等操作
                    exit();
                }
            }
        }
    }

	public function dispatchLoopStartup(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response) {


    }

	public function preDispatch(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response) {}

	public function postDispatch(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response) {}

	public function dispatchLoopShutdown(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response) {
        //显示模板
        SmartyAdapter::display();
    }
    public function getUserIp(){

        $realip = '0.0.0.0';

        if (isset($_SERVER)){
            if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])){
                $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
                /* 取X-Forwarded-For中第一个非unknown的有效IP字符串 */
                foreach ($arr AS $ip){
                    $ip = trim($ip);

                    if ($ip != 'unknown'){
                        $realip = $ip;

                        break;
                    }
                }
            }
            elseif (isset($_SERVER['HTTP_CLIENT_IP'])){
                $realip = $_SERVER['HTTP_CLIENT_IP'];
            }
            else{
                if (isset($_SERVER['REMOTE_ADDR'])){
                    $realip = $_SERVER['REMOTE_ADDR'];
                }
                else{
                    $realip = '0.0.0.0';
                }
            }
        }
        else{
            if (getenv('HTTP_X_FORWARDED_FOR')){
                $realip = getenv('HTTP_X_FORWARDED_FOR');
            }
            elseif (getenv('HTTP_CLIENT_IP')){
                $realip = getenv('HTTP_CLIENT_IP');
            }
            else{
                $realip = getenv('REMOTE_ADDR');
            }
        }

        preg_match("/[\d\.]{7,15}/", $realip, $onlineip);
        $realip = !empty($onlineip[0]) ? $onlineip[0] : '0.0.0.0';

        return $realip;
    }

}
